package com.android.jingweiapp.fragment.publicinfo;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.jingweiapp.R;
import com.android.jingweiapp.XMLPhase.GetLeaderXMLPhase;
import com.android.jingweiapp.activity.DetailTextActivity;
import com.android.jingweiapp.adapter.NormalRecyclerViewAdapter;
import com.android.jingweiapp.bean.LeaderBean;
import com.android.jingweiapp.util.Contacts;
import com.android.jingweiapp.util.HttpUtil;
import com.android.jingweiapp.widget.MyToast;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 领导信息
 */
public class LeaderFragment extends Fragment {

    @Bind(R.id.keyWordView)
    RecyclerView recycleView;

    private ArrayList<Parcelable> leaderBeans;
    private NormalRecyclerViewAdapter adapter;

    public LeaderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_leader, container, false);
        ButterKnife.bind(this, view);
        new MyAsyncTask().execute();
        leaderBeans = new ArrayList<>();
        adapter = new NormalRecyclerViewAdapter(getActivity(), leaderBeans, Contacts.TYPE_ADAPTER.TYPE_LEADER);

        recycleView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recycleView.setAdapter(adapter);
        adapter.setOnItemClickListener(new NormalRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, Parcelable data) {
                startActivity(new Intent(getActivity(), DetailTextActivity.class)
                        .putExtra(Contacts.KEY.KEY_OBJECT, data)
                        .putExtra(Contacts.KEY.KEY_TYPE, Contacts.TYPE.LEADER_INFO));
            }
        });
        return view;
    }


    private void dealData(String result) {
        if (TextUtils.isEmpty(result)) {
            MyToast.getInstance().show("出错了，请重试");
            return;
        }
        if (result.equals("nodata")) {
            MyToast.getInstance().show("暂无数据");
            return;
        }
        try {
            Gson gson = new Gson();
            JSONObject jsonObject = new JSONObject(result);
            JSONArray array = jsonObject.getJSONArray("data");
            for (int i = 0; i < array.length(); i++) {
                leaderBeans.add(gson.fromJson(array.getJSONObject(i).toString(),
                        LeaderBean.class));
            }
            adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    class MyAsyncTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    GetLeaderXMLPhase.createParam(getActivity()),
                    new GetLeaderXMLPhase());
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            dealData(s);
        }
    }
}
